<?php

function msnqq_getErrMsg($ierr)
{
	switch($ierr)
	{
		case 0:     return "Success";
		case -2:    return "data is not exist";
		case -1:    return "System error";
		case -3:    return "input args is error";
		default:    return "Undefined error: $ierr";
	}
}

class MSNQQ{
	var $sock;
        var $retry;
	function __construct($port = 7080, $retry = 3){
                $this->retry = $retry;
		$this->sock = @pfsockopen("10.62.162.75", $port, $errno, $errstr);
		if (!$this->sock) {
			error_log("ERROR: $errno - $errstr");
			return;
		}
		stream_set_timeout($this->sock, 1, 400);
	} 

	function __destruct() { } 
	function friends( $user, $type = 1, $qq =  true){
		$ret = $this->inner_friends($user, $type, $qq); 
		$try = 0;
		while($ret === false && $try < $this->retry)
		{
			$ret = $this->inner_friends($user, $type, $qq); 
			$try++;
		}
		return $ret;
	}
	function status( $type ){
		$ret = $this->inner_status( $type ); 
		$try = 0;
		while(!isset($ret['1'])  && $try < $this->retry)
		{
			$ret = $this->inner_status(); 
			$try++;
		}
		return $ret;
	}
	
	function reset( $arr, $qq){
		if(is_array($arr) && sizeof($arr) > 0)
		{
			$isize = sizeof($arr);
                        if($qq)
                           $hash = time(NULL)%2 + 1;
                        else
                           $hash = time(NULL)%2 + 3;
                        $input = "P".pack("I", $hash )."\002";

			$input .= "y\004".pack("I",$isize);
			foreach($arr as $k=>$v)
				$input .= pack("I",strlen($k)).$k;
			fwrite($this->sock, $input);
			return true;
		}
		return false;
	}
	function example($counts, $qq ){
                        if($qq)
                           $hash = time(NULL)%2 + 1;
                        else
                           $hash = time(NULL)%2 + 3;
                        $input = "P".pack("I", $hash )."\002";
			$input .= "y\007";
			$input .= pack("I", $counts);
			fwrite($this->sock, $input);
			return true;
	}

	function reload_by_msnqq($users, $qq ){
		if($qq)
			$hash = time(NULL)%2 + 1;
		else
			$hash = time(NULL)%2 + 3;
		$input = "P".pack("I", $hash )."\002";
		$input .= "y\004";
		$user_counts = sizeof($users);
		$input .= pack("I",$user_counts);
		foreach($users as $k=>$v)
			$input .=pack("I", strlen($k)).$k;
		fwrite($this->sock, $input);
                return true;
	}	


	function reload_by_nid($users,$qq ){
		if($qq)
			$hash = time(NULL)%2 + 1;
		else
			$hash = time(NULL)%2 + 3;
		$input = "P".pack("I", $hash )."\002";
		$input .= "y\005";
		$user_counts = sizeof($users);
		$input .= pack("I",$user_counts);
		foreach($users as $k=>$v)
			$input .=pack("I", strlen($k)).$k;
		fwrite($this->sock, $input);
                return true;
	}	

	function inner_friends($user, $type, $qq){
		if($qq)
			$hash = time(NULL)%2 + 1;
		else
			$hash = time(NULL)%2 + 3;
		$input = "P".pack("I", $hash )."\001";
		if($type == 2)
			$input .= "y\002";
		else if($type == 3)
			$input .= "y\003";
		else
			$input .= "y\001";

		$input .= pack("I",strlen($user)).$user;
		fwrite($this->sock, $input);
		$readlen = $READ = 102400;
		$response = fread($this->sock, $READ);
		//var_dump($response);	         
		$arr = array();
              		if(isset($response{7}))
			{
				$sub = substr($response, 0, 8);
				$unpacked = unpack('I2',$sub);
//var_dump($unpacked);
				if(isset($unpacked['1']) && isset($unpacked['2']))
				{
					if($unpacked['2'] != 0)
					{
                                                error_log(msnqq_getErrMsg($unpacked['2']));
                                        	return $unpacked['2']; // return errorcode;
                                        }
					$left = $unpacked['1'] - strlen($response);

					while ( $left > 0 && !feof($this->sock) )
					{
						$contstr = fread ( $this->sock,  $left );
						if (is_string($contstr) && isset($contstr{0}))
						{
							$response .= $contstr;
							$left -= strlen($contstr);
						}
						else
							break;
					}

					//echo "read = "; var_dump($response);
					$idx = 8;
					$sub = substr($response, $idx , 4);
					if(!isset($sub{3})){
						error_log("get friends counts data error!");
						return false;
					}

					$unpacked = unpack('I',$sub);
					// echo "counts = "; var_dump($unpacked);
					$idx += 4;
					$counts = $unpacked['1'];
					if($counts == 0)
						return $arr;
					for($j = 0; $j<$counts ; $j++)
					{
						$str = substr($response, $idx, 4);
						if(!isset($str{3})){
							error_log("get firends name length data error");
							return false;
						}
						$unpacked = unpack('Iint', $str);
						$idx += 4;
						$uname = substr($response, $idx, $unpacked['int']);
						$idx += $unpacked['int'];
                                                // var_dump($uname);

                                             	$str = substr($response, $idx, 4);
						if(!isset($str{3})){
							error_log("get active number error!");
							return false;
						}
						$unpacked = unpack('Iint', $str);
                                               //  var_dump($unpacked);

						$idx += 4;
						if($unpacked['int'] > 0)
							$uvalue = substr($response, $idx, $unpacked['int']);
						else
							$uvalue = "";
						//  var_dump($uvalue); 
 			                        $idx += $unpacked['int'];
						$arr[$uname] = $uvalue;
					}
					if(sizeof($arr) < $counts)
						return false;
				}
			}
			else
				return false; //echo "return error!";
			return $arr;
	}
	function inner_status($qq){
                        if($qq)
                           $hash = time(NULL)%2 + 1;
                        else
                           $hash = time(NULL)%2 + 3;
                        $input = "P".pack("I", $hash )."\001";
			$input .= "y\006status";
			fwrite($this->sock, $input);
			$readlen = $READ = 1024;
			$response = fread($this->sock, $READ);
	                $arr = array();
              		if(isset($response{7}))
			{
				$sub = substr($response, 0, 8);
				$unpacked = unpack('I2',$sub);
				if(isset($unpacked['1']) && isset($unpacked['2']))
				{
					if($unpacked['2'] != 0)
						return false; // return errorcode;
				//	echo "read = "; var_dump($response);
					$idx = 8;
					$sub = substr($response, $idx , 12);
                                        if(!isset($sub{11}))
                                            return false;
					$arr = unpack('I3',$sub);
				}
				if(sizeof($arr)< 1)
					return false;
			}
			else
				return false; //echo "return error!";
			return $arr;
	}

}


?>
